package com.py.credit.service.impl;

import com.py.credit.common.Page;
import com.py.credit.dao.DeptDataDao;
import com.py.credit.entity.*;
import com.py.credit.service.DeptDataService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeptDataServiceImpl implements DeptDataService {

    @Autowired
    private DeptDataDao deptDataDao;

    @Override
    public List<DeptData> getListForPage(String deptId, String startTime, String endTime, Page page) {
        return deptDataDao.selectListForPage(deptId, startTime, endTime, page);
    }

    @Override
    public List<Map<String, Object>> selectForLhbm(int gzid) {
        return deptDataDao.selectForLhbm(gzid);
    }

    @Override
    public DeptData selectByAjbh(String ajbh) {
        return deptDataDao.selectByAjbh(ajbh);
    }

    @Override
    public List<Measure> selectCsByAjbh(int gzId) {
        return deptDataDao.selectCsByAjbh(gzId);
    }

    @Override
    public Companymess selectByTybm(String tybm, String type) {
        if (type.equals("C")) {
            return deptDataDao.selectQYXXByTybm(tybm);
        } else {
            return deptDataDao.selectGRXXByTybm(tybm);
        }
    }

    @Override
    public void excelExport(HttpServletResponse response, String startTime, String endTime, String deptId, String type) throws IOException {
        queryTableExecle(response, startTime, endTime, deptId, type);
    }

    private void queryTableExecle(HttpServletResponse response, String startTime, String endTime, String deptId, String type) throws IOException {
        // 生成提示信息
        response.setContentType("application/vnd.ms-excel");
        HSSFWorkbook workbook = null;
        OutputStream fOut = null;
        String codedFileName = "部门数据";
        // 进行转码，使其支持中文文件名
        codedFileName = URLEncoder.encode(codedFileName, "UTF-8");
        response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
        // 产生工作簿对象
        workbook = new HSSFWorkbook();
        // 产生工作表对象
        HSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(0, "部门数据");
        HSSFRow row = null;
        HSSFCell cell = null;

        String[] strBT = {"事件标号", "联合奖惩规则", "事件类别", "主体类型", "主体名称", "配合部门", "事件状态", "奖惩期限"};
        String[] strNR = {"ajbh", "gzmc", "jclb", "type", "ztmc", "lhbm", "status", "repairTime"};
        List<DeptData> list = new ArrayList<DeptData>();
        if (type.equals("F")) {
            list = deptDataDao.getReportList(startTime, endTime, deptId);
        } else {
            list = deptDataDao.getReportPhList(startTime, endTime, deptId);
        }

        List<Map<String, Object>> listNew = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> lhbms = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < list.size(); i++) {
            int gzid = list.get(i).getGzId();
            lhbms = selectForLhbm(gzid);
            String lhbm = "";
            for (Map<String, Object> map : lhbms) {
                lhbm += map.get("deptName") + ",";
            }
            list.get(i).setLhbm(lhbm.substring(0, lhbm.length() - 1));

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("ajbh", list.get(i).getAjbh());
            map.put("gzmc", list.get(i).getGzmc());
            if (list.get(i).getJclb().equals("C")) {
                list.get(i).setJclb("联合惩戒");
            } else if (list.get(i).getJclb().equals("J")) {
                list.get(i).setJclb("联合激励");
            }

            map.put("jclb", list.get(i).getJclb());
            if (list.get(i).getType().equals("P")) {
                list.get(i).setType("自然人");
            } else if (list.get(i).getType().equals("C")) {
                list.get(i).setType("法人");
            }
            map.put("type", list.get(i).getType());
            map.put("ztmc", list.get(i).getZtmc());
            map.put("lhbm", list.get(i).getLhbm());

            if (list.get(i).getStatus().equals("G")) {
                list.get(i).setStatus("已失效");
            } else if (list.get(i).getStatus().equals("J")) {
                list.get(i).setStatus("进行中");
            } else if (list.get(i).getStatus().equals("O")) {
                list.get(i).setStatus("已移出");
            }
            map.put("status", list.get(i).getStatus());
            map.put("repairTime", list.get(i).getRepairTime());
            listNew.add(map);

        }

        //创建标题行
        row = sheet.createRow(0);
        for (int i = 0; i < strBT.length; i++) {
            cell = row.createCell(i);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(strBT[i]);
        }
        for (int i = 0; i < listNew.size(); i++) {
            row = sheet.createRow((int) i + 1);//创建一行
            for (int j = 0; j < strBT.length; j++) {
                cell = row.createCell(j);//创建一列
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                boolean res;
                res = listNew.get(i).get(strNR[j]) instanceof String;
                if (res) {
                    cell.setCellValue((String) listNew.get(i).get(strNR[j]));
                } else {
                    BigDecimal value = ((BigDecimal) listNew.get(i).get(strNR[j]));
                    cell.setCellValue(value.toString());
                }
            }
        }
        listNew = null;
        fOut = response.getOutputStream();
        workbook.write(fOut);
        workbook.close();
        fOut.flush();
        fOut.close();
    }

    @Override
    public List<Feedback> getFeedbackDetail(String ajbh, String deptId) {
        return deptDataDao.getFeedbackDetail(ajbh, deptId);
    }

    @Override
    public Person selectZrrInfo(String idcard) {
        return deptDataDao.selectZrrInfo(idcard);
    }

    @Override
    public List<NumObj> deptReportNum(String deptId, String startTime, String endTime) {
        return deptDataDao.deptReportNum(deptId, startTime, endTime);
    }

    @Override
    public List<DeptNum> selectCjInfo(String deptId, String startTime, String endTime) {
        return deptDataDao.selectCjInfo(deptId, startTime, endTime);
    }

    @Override
    public List<DeptNum> selectCjOutInfo(String deptId, String startTime, String endTime) {
        return deptDataDao.selectCjOutInfo(deptId, startTime, endTime);
    }

    @Override
    public List<DeptNum> selectJlInfo(String deptId, String startTime, String endTime) {
        return deptDataDao.selectJlInfo(deptId, startTime, endTime);
    }

    @Override
    public List<DeptNum> selectJlOutInfo(String deptId, String startTime, String endTime) {
        return deptDataDao.selectJlOutInfo(deptId, startTime, endTime);
    }

    @Override
    public int selectDeptFq(String deptId, String startTime, String endTime) {
        return deptDataDao.selectDeptFq(deptId, startTime, endTime);
    }

    @Override
    public int selectDeptXy(String deptId, String startTime, String endTime) {
        return deptDataDao.selectDeptXy(deptId, startTime, endTime);
    }

    @Override
    public int selectDeptSj(String deptId, String startTime, String endTime) {
        return deptDataDao.selectDeptSj(deptId, startTime, endTime);
    }

    @Override
    public int selectDeptLj(String deptId, String startTime, String endTime) {
        return deptDataDao.selectDeptLj(deptId, startTime, endTime);
    }

    @Override
    public int selectSjGz(String deptId, String startTime, String endTime) {
        return deptDataDao.selectSjGz(deptId, startTime, endTime);
    }

    @Override
    public List<String> selectCfZrr(String deptId, String startTime, String endTime) {
        return deptDataDao.selectCfZrr(deptId, startTime, endTime);
    }

    @Override
    public List<NumObj> deptPhReportNum(String deptId, String startTime, String endTime) {
        return deptDataDao.deptPhReportNum(deptId, startTime, endTime);
    }

    @Override
    public List<DeptNum> selectJcRecieve(String deptId, String startTime, String endTime) {
        // TODO Auto-generated method stub
        return deptDataDao.selectJcRecieve(deptId, startTime, endTime);
    }

    @Override
    public List<DeptNum> selectJcFeedback(String deptId, String startTime, String endTime) {
        // TODO Auto-generated method stub
        return deptDataDao.selectJcFeedback(deptId, startTime, endTime);
    }

    @Override
    public int selectJcRecieveTotal(String deptId, String startTime, String endTime) {
        // TODO Auto-generated method stub
        return deptDataDao.selectJcRecieveTotal(deptId, startTime, endTime);
    }

    @Override
    public int selectJcFeedbackTotal(String deptId, String startTime, String endTime) {
        // TODO Auto-generated method stub
        return deptDataDao.selectJcFeedbackTotal(deptId, startTime, endTime);
    }

    @Override
    public int selectOthersFeedbackTotal(String deptId, String startTime, String endTime) {
        // TODO Auto-generated method stub
        return deptDataDao.selectOthersFeedbackTotal(deptId, startTime, endTime);
    }

    @Override
    public int selectOtherSjGz(String deptId, String startTime, String endTime) {
        // TODO Auto-generated method stub
        return deptDataDao.selectOtherSjGz(deptId, startTime, endTime);
    }

    @Override
    public List<DeptData> getPhListForPage(String deptId, String startTime, String endTime, Page page) {
        // TODO Auto-generated method stub
        return deptDataDao.getPhListForPage(deptId, startTime, endTime, page);
    }
}
